using System;
using System.Collections.Generic;
using UnityEngine;

public interface IGameEvent { }

public class PlayerDashedEvent : IGameEvent { }
public class WeaponEquippedEvent : IGameEvent 
{
    public Weapon weapon;
    public WeaponEquippedEvent(Weapon weapon)
    {
        this.weapon = weapon;
    }
}

public class WeaponReloadStartedEvent : IGameEvent 
{
    public Weapon weapon;
    public WeaponReloadStartedEvent(Weapon weapon)
    {
        this.weapon = weapon;
    }
}

public class WeaponReloadFinishedEvent : IGameEvent 
{
    public Weapon weapon;
    public WeaponReloadFinishedEvent(Weapon weapon)
    {
        this.weapon = weapon;
    }
}

public class MeleeAttackStartedEvent : IGameEvent 
{
    public MeleeWeapon weapon;
    public MeleeAttackStartedEvent(MeleeWeapon weapon)
    {
        this.weapon = weapon;
    }
}

public class RangedAttackFiredEvent : IGameEvent 
{
    public RangedWeapon weapon;
    public RangedAttackFiredEvent(RangedWeapon weapon)
    {
        this.weapon = weapon;
    }
}

public class EnemyAttackedEvent : IGameEvent 
{
    public EnemyAI enemy;
    public EnemyAttackedEvent(EnemyAI enemy)
    {
        this.enemy = enemy;
    }
}

public static class EventManager
{
    private static Dictionary<Type, Action<IGameEvent>> eventHandlers = new Dictionary<Type, Action<IGameEvent>>();

    public static void RegisterEvent<T>(Action<T> handler) where T : IGameEvent
    {
        Type eventType = typeof(T);
        if (!eventHandlers.ContainsKey(eventType))
        {
            eventHandlers[eventType] = (e) => { };
        }

        eventHandlers[eventType] += (e) => handler((T)e);
    }

    public static void UnregisterEvent<T>(Action<T> handler) where T : IGameEvent
    {
        Type eventType = typeof(T);
        if (eventHandlers.ContainsKey(eventType))
        {
            eventHandlers[eventType] -= (e) => handler((T)e);
        }
    }

    public static void TriggerEvent(IGameEvent gameEvent)
    {
        Type eventType = gameEvent.GetType();
        if (eventHandlers.ContainsKey(eventType))
        {
            eventHandlers[eventType]?.Invoke(gameEvent);
        }
    }
}